﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Orchard.Environment.Extensions;
using Orchard.Commands;
using Orchard.ContentManagement;
using Orchard.Security;
using Orchard.Settings;
using Orchard.Core.Navigation.Services;
using Downplay.Mechanics.Services;
using Orchard.Core.Common.Models;
using Orchard;
using Downplay.Mechanics.Models;

namespace Downplay.Mechanics.Commands
{
    public class MechanicsCommands : DefaultOrchardCommandHandler
    {
        private readonly IContentManager _contentManager;
        private readonly IMembershipService _membershipService;
        private readonly IMechanicsService _mechanics;

        public MechanicsCommands(
            IContentManager contentManager,
            IMembershipService membershipService,
            IMechanicsService mechanics,
            IOrchardServices services)
        {
            _contentManager = contentManager;
            _membershipService = membershipService;
            _mechanics = mechanics;
            Services= services;
        }

        public IOrchardServices Services { get; set; }

        [OrchardSwitch]
        public string ContentTypes { get; set; }

        [CommandName("connectors publish")]
        [CommandHelp("connectors publish [/ContentTypes:<types>]\r\n\t" + "Attempts to publish all connectors of specified type(s). This is an upgrade option, typically if you've changed a connector to be Draftable.")]
        public void PublishConnectors()
        {

            var types = String.IsNullOrWhiteSpace(ContentTypes)?null:ContentTypes.Split(new[]{','},StringSplitOptions.RemoveEmptyEntries);
            var q = _contentManager.Query<ConnectorPart>(VersionOptions.AllVersions,types);
            foreach (var c in q.List())
            {
                _contentManager.Publish(c.ContentItem);
            }
        }

    }
}